<?php
$username = $_POST['username'];
$pwd = $_POST['pwd'];

include('./connect.php');

if ($conn->connect_error){
    echo '数据库连接失败！';
    exit(0);
}else{
    if ($username == ''){
        echo '<script>alert("请输入用户名！");history.go(-1);</script>';
        exit(0);
    }
    if ($pwd == ''){
        echo '<script>alert("请输入密码！");history.go(-1);</script>';
        exit(0);
    }
    $sql = "select username,pwd,petname from user where username = '$username' and pwd = '$pwd'";
    $result = $conn->query($sql);
    if (!$result) {
        echo '<script>alert("输入错误");</script>';
        // printf("Error: %s\n", mysqli_error($conn));//打印错误信息
        exit();
    }
    $number = mysqli_num_rows($result);
    $row = mysqli_fetch_assoc($result);
    if ($number) {
        session_start();
        $_SESSION['username'] = $row['username'];
        $_SESSION['petname'] = $row['petname'];
        echo "<script>localStorage.setItem('kusername','$username')</script>";
        echo "<script>localStorage.setItem('kpassword','$pwd')</script>";
        echo "<script>location = 'index.php'</script>";
    } else {
        echo '<script>alert("用户名或密码错误！");history.go(-1);</script>';
    }
}
?>